package com.mstore.dao;

import java.util.List;

import com.mstore.pojo.BgiveRule;

public class BgiveRuleDao extends SuperDao {

	/**
	 * 查看积分规则
	 */
	public List<Object> watchBgiveRule(BgiveRule bGiveRule) {
		String hql = "from  BgiveRule where  eaId = ?";
		Object[] values = new Object[] { bGiveRule.getEaId() };
		return super.getObject(hql, values);
	}

	/**
	 * 删除积分规则
	 */
	public void delBgiveRule(BgiveRule bGiveRule) {
		super.delete(bGiveRule);
	}

	/**
	 * 获取一个积分规则对象
	 */
	public Object getOneBgiveRule(BgiveRule bGiveRule) {
		String hql = "FROM BgiveRule WHERE id = ?";
		Object[] values = new Object[] { bGiveRule.getId() };
		List<Object> list = super.getObject(hql, values);
		if (list.size() != 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 保存积分规则
	 */
	public void saveBgiveRule(BgiveRule bGiveRule) {
		super.save(bGiveRule);
	}

	/**
	 * 修改积分规则
	 */
	public void modifyBgiveRule(BgiveRule bGiveRule) {
		/*String sql = "update b_give_rule set " + "name='" + bGiveRule.getName()
				+ "',from_time = '" + bGiveRule.getFromTime()
				+ "', to_time = '" + bGiveRule.getToTime()
				+ "', raise_value = '" + bGiveRule.getRaiseValue()
				+ "', type= '" + bGiveRule.getType() + "', state= '"
				+ bGiveRule.getState() + "'  WHERE id = " + bGiveRule.getId();
		super.executeUpdate(sql);*/
		
		StringBuilder sb=new StringBuilder();
		sb.append( "update b_give_rule set name='");
		sb.append(bGiveRule.getName());
		sb.append("',from_time = '");
		sb.append(bGiveRule.getFromTime());
		sb.append("', to_time = '");
		sb.append(bGiveRule.getToTime());
		sb.append("', raise_value = '");
		sb.append(bGiveRule.getRaiseValue());
		sb.append("', type= '");
		sb.append(bGiveRule.getType());
		sb.append("', state= '");
		sb.append(bGiveRule.getState());
		sb.append("'  WHERE id = ");
		sb.append(bGiveRule.getId());
		
		super.executeUpdate(sb.toString());
	}

	/**
	 * 判断当前时间是否在积分活动时间内
	 */
	@SuppressWarnings("unchecked")
	public List<Object> selectByTime(BgiveRule bGiveRule) {
		/*
		 * String sql =
		 * "SELECT ID, CREATE_TIME , EA_ID, FROM_TIME, TO_TIME, RAISE_VALUE, TYPE, STATE  FROM  b_give_rule WHERE (SELECT NOW() AS 'YYYY-MM-DD HH:MM:SS'  ) BETWEEN   FROM_TIME AND TO_TIME  AND eaId="
		 * + bGiveRule.getEaId(); return (List<Object>) super.executeSql(sql);
		 */
		
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ID, CREATE_TIME , EA_ID, FROM_TIME, TO_TIME, RAISE_VALUE, TYPE, STATE  FROM  b_give_rule WHERE (SELECT NOW() AS 'YYYY-MM-DD HH:MM:SS'  ) BETWEEN   FROM_TIME AND TO_TIME  AND eaId=");
		sb.append(bGiveRule.getEaId());
		return (List<Object>) super.executeSql(sb.toString());
	}

	/**
	 * 通过eaId ，state来查询
	 */
	public List<Object> selectByState(BgiveRule bGiveRule) {

		String hql = "from  BgiveRule  where  state=?  and  eaId=?";
		Object[] values = new Object[] { bGiveRule.getState(),
				bGiveRule.getEaId() };
		return super.getObject(hql, values);
	}

	public Object findBgiveRuleByType(BgiveRule bGiveRule) {
		String hql = "FROM BgiveRule WHERE eaId = ? and type = ?";
		Object[] values = new Object[] { bGiveRule.getEaId(),
				bGiveRule.getType() };
		List<Object> list = super.getObject(hql, values);
		if (list.size() != 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 修改
	 */
	public void modifyGiveValue(BgiveRule bGiveRule) {
		/*String sql = "update b_give_rule set give_value = "
				+ bGiveRule.getGiveValue() + " WHERE id = " + bGiveRule.getId();
		super.executeUpdate(sql);*/
		
		StringBuilder sb=new StringBuilder();
		sb.append("update b_give_rule set give_value =");
		sb.append(bGiveRule.getGiveValue());
		sb.append(" WHERE id=");
		sb.append(bGiveRule.getId());
		super.executeUpdate(sb.toString());
	}
	
	/**
	 * 判断当前时间是否在积分活动时间内
	 */
	public Object getGiveRuleByTime(String time, Long eaId) {
		StringBuilder bu = new StringBuilder();
		bu.append("FROM BgiveRule WHERE ('");
		bu.append(time);
		bu.append("' BETWEEN fromTime AND toTime) AND eaId = ");
		bu.append(eaId);
		bu.append(" AND state = 1");
		List<Object> objects = find(bu.toString());
		if (objects.isEmpty()) {
			return null;
		}
		return objects.get(0);
	}
	
	/**
	 * 查询注册时送积分规则
	 */
	public Object getEaGiveRule(Long eaId) {
		String sql = "from BgiveRule where eaId = ? and type = 1 and state = 1";
		List<Object> objects = getObject(sql, new Object[]{ eaId });
		if (objects.isEmpty()) {
			return null;
		}
		return objects.get(0);
	}
}
